<!--
 * @author gaohailan
 -->
<template>
  <div class="app-container reason-list">
    <el-collapse-transition>
      <div class="exam-search separate">
        <div class="exam-search-inner">
          <el-row :gutter="20">
            <el-col :span="5">
              <div class="search-inner-item">
                <div class="item-label">征集活动名称</div>
                <el-input
                    v-model.trim="listQuery.activityName"
                    clearable
                    class="filter-item"
                    style="width: 100%"
                    placeholder="请输入征集活动名称搜索"
                />
              </div>
            </el-col>
            <el-col :span="5">
              <div class="search-inner-item">
                <div class="item-label">状态</div>
                <el-select
                    v-model="listQuery.state"
                    clearable
                    placeholder="请选择"
                    style="width: 100%"
                    class="filter-item"
                >
                  <el-option key="0" :value="0" label="启用"/>
                  <el-option key="1" :value="1" label="禁用"/>
                </el-select>
              </div>
            </el-col>
            <el-col :span="9">
              <div class="search-inner-item">
                <div class="item-label">征集时间</div>
                <el-date-picker
                    v-model="listQuery.time"
                    style="width: 100%"
                    type="datetimerange"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    range-separator="至"
                    start-placeholder="开始时间"
                    end-placeholder="结束时间"
                />
              </div>
            </el-col>
          </el-row>
        </div>
        <div class="exam-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">{{
              $t('table.search')
            }}
          </el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn"
          >重置
          </el-button
          >
        </div>
      </div>
    </el-collapse-transition>

    <div class="common-btn">
      <div class="btn-title">
        <span>建议活动</span>
      </div>
      <div class="btn-btns">
        <el-button style="margin-left: 10px" @click="handleCreate"
        >新建活动
        </el-button
        >
        <el-button style="margin-left: 10px" @click="handleDeletes"
        >批量删除
        </el-button
        >
      </div>
    </div>
    <div class="table">
      <el-table
          v-loading="listLoading"
          :data="honorList"
          class="tableScope"
          :header-cell-style="rowClass"
          style="width: 100%"
          @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"/>
        <el-table-column
            prop="activityName"
            label="征集活动名称"
            show-overflow-tooltip
        />

        <el-table-column
            prop="activityStartTime"
            label="征集时间"
            show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span
            >{{
                scope.row.activityStartTime
                    ? scope.row.activityStartTime.substring(0, 10)
                    : ''
              }}~{{
                scope.row.activityEndTime
                    ? scope.row.activityEndTime.substring(0, 10)
                    : ''
              }}</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="crtUser" label="创建人" show-overflow-tooltip/>
        <el-table-column
            prop="state"
            label="状态"
            width="80"
            show-overflow-tooltip
        >
          <template slot-scope="scope">
            <span>{{ scope.row.state == '0' ? '启用' : '禁用' }}</span>
          </template>
        </el-table-column>
        <el-table-column
            label="操作"
            fixed="right"
            align="center"
            width="200"
            class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleUpdate(scope.row)"
            >编辑
            </el-button
            >
            <el-button type="text" size="mini" @click="handelDel(scope.row)"
            >删除
            </el-button
            >
            <el-button type="text" size="mini" @click="selectionFn(scope.row)"
            >审核
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
    />
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
// import {
//   SuggestActiveQueryPage,
//   SuggestActiveDelete,
// } from "@/api/reasonable/index";
export default {
  name: 'ReasonableMain',
  components: {Pagination},
  data() {
    return {
      listLoading: false,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        activityName: '',
        state: '',
        time: '',
      },
      multipleSelection: [],
      honorList: [],
    }
  },
  created() {
    // this.getList();
  },
  methods: {
    selectionFn(item) {
      this.$router.push({
        path: '/reasonable/reasonableAnswer',
        name: 'ReasonableAnswer',
        query: {id: item.id, rowData: item},
      })
    },
    rowClass({row, rowIndex}) {
      return {
        padding: '5px 0',
        height: '20px',
        background: 'rgba(247,247,248,1)',
      }
    },
    handleSelectionChange(arr) {
      this.multipleSelection = arr
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resettingBtn() {
      this.listQuery.activityName = ''
      this.listQuery.state = '',
          this.listQuery.time = ''
      this.getList()
    },
    handleCreate() {
      this.$router.push({path: '/reasonable/edit', name: 'ReasonableEdit'})
    },
    handleUpdate(item) {
      this.$router.push({
        path: '/reasonable/edit',
        name: 'ReasonableEdit',
        query: {id: item.id, rowData: item},
      })
    },
    handelDel(row) {
      this.deleteHonor(row.id)
    },
    handleDeletes() {
      const arr = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach((item) => {
          arr.push(item.id)
        })
        var ids = arr.join(',')
        this.deleteHonor(ids)
      } else {
        this.$message({
          message: '请选择需要删除的数据！',
          type: 'warning',
        })
      }
    },
    getList(obj) {
      this.listLoading = true
      var query = {
        activityName: this.listQuery.activityName,
        state: this.listQuery.state,
        activityStartTime: this.listQuery.time[0],
        activityEndTime: this.listQuery.time[1],
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
      }
      return new Promise((resolve, reject) => {
        SuggestActiveQueryPage(query)
            .then((response) => {
              this.honorList = response.data.data.rows
              this.total = response.data.data.total
              setTimeout(() => {
                this.listLoading = false
              }, 1 * 1000)
            })
            .catch((error) => {
              reject(error)
              setTimeout(() => {
                this.listLoading = false
              }, 1 * 1000)
            })
      })
    },
    deleteHonor(ids) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
          .then(() => {
            return new Promise((resolve, reject) => {
              SuggestActiveDelete({ids: ids})
                  .then(() => {
                    this.$notify({
                      title: '成功',
                      message: '删除成功',
                      type: 'success',
                      duration: 2000,
                    })
                    this.getList()
                  })
                  .catch((error) => {
                    reject(error)
                  })
            })
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
    },
  },
}
</script>
<style rel="stylesheet/scss" lang="scss">
$dark_gray: #bfced6;
$dark_main: #18a5fd;
$mainColor: #18a5fd;
.reason-list {
  .common-search {
    background-color: #fff;
    padding: 0 20px;
    margin-bottom: 10px;

    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .course-search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .course-search-more {
        font-size: 14px;
        font-weight: 500;
        color: $dark_main;
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .course-search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: "";
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .course-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .exam-search {
    background-color: #fff;
    padding: 0 20px;

    .svg-container {
      padding: 0;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .exam-search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .exam-search-more {
        font-size: 14px;
        font-weight: 500;
        color: $mainColor;
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .exam-search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: "";
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .exam-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .common-btn {
    display: flex;
    height: 36px;
    line-height: 36px;
    margin-bottom: 10px;

    .btn-title {
      flex: 1;
      font-size: 16px;
      font-weight: bold;
      font-family: PingFangSC-Medium;
      color: rgba(51, 51, 51, 1);
    }
  }

  .el-dialog {
    .el-dialog__body {
      padding: 20px;
    }

    .el-dialog__footer {
      border-top: 1px solid #e2e3e8;
      padding: 15px 20px;
    }
  }

}
</style>
